layui.define(['table', 'form', 'selectM', 'selectN', 'laydate'], function(exports){
  var $ = layui.$,selectM = layui.selectM, selectN = layui.selectN, form = layui.form, laydate = layui.laydate;
  
  var placeId = $('input[name="id"]').val();
  var typeCodes = $('#typeCodesValue').val();
  var areaCode = $('#areaCodeValue').val();

  $('input[name="thumbnailImg"]').jfFileManager({
      resDomain:layui.setter.resDomain,
      token:layui.setter.token,
      multiple:false,
      parent:parent
  });

  selectN({
      elem: '#areaCode'
      , name: 'areaCode'
      , search: [true, true]
      , selected: areaCode
      , data: layui.setter.webRoot + '/common/china/area/tree/select.data'
      , field: {
          idName: 'id',
          titleName: 'name',
          statusName: 'status',
          childName: 'children'
      }
  });

  selectN({
      elem: '#typeCodes'
      , name: 'typeCodes'
      , search: [true, true]
      , selected: typeCodes
      , data: layui.setter.webRoot + '/place/physical_store/type/select.data'
      , field: {
          idName: 'code',
          titleName: 'name',
          statusName: 'status',
          childName: 'children'
      }
  });

  laydate.render({
      elem: '#businessTimeStr'
      , type:'time'
      , format:'HH:mm'
      , range: true
  });

  $('#btnOpenAMap').click(function () {
      parent.layer.open({
          type: 2
          , title: '地图'
          , content: layui.setter.webRoot + '/place/gps/map.html?placeId=' + placeId
          , maxmin: true
          , shade: 0
          , area: ['60%', '80%']
          , btn: ['确定', '取消']
          , yes: function (index, layero) {
              var iframeWindow = parent.window['layui-layer-iframe' + index]
                  , submit = layero.find('iframe').contents().find("#LAY-form-submit");
              iframeWindow.layui.form.on('submit(LAY-form-submit)', function (data) {
                  var field = data.field;
                  $('input[name="longitude"]').val(field.longitude);
                  $('input[name="latitude"]').val(field.latitude);
                  parent.layer.close(index);
              });
              submit.trigger('click');
          }
          , zIndex: layer.zIndex
      });
  });

  $('#userId').select2({
      placeholder: "请选择网点归属代理",
      width: '300px',
      theme: 'bootstrap',
      allowClear : true,
      ajax: {
          url: layui.setter.webRoot + '/agent/select.data',
          method: 'post',
          delay: 250,
          processResults: function (result) {
              if (result.code == 200) {
                  return {
                      pagination: {more: result.body.more},
                      results: result.body.data || []
                  };
              } else {
                  return {results: []};
              }
          }
      }
  });
  exports('physical_store_edit', {})
});